import mixin from '../../AddParkingLot/AddParkingLotComponents/rule/opt';
import specialRule from './specialRule';
import utils from '@/mixins/utils.js';
import { deepCopyObj } from '../../../../assets/js/help';
export default {
	mixins: [mixin, specialRule, utils],
	data() {
		return {
			parkBaseInfo: null,
			domain: null,
			tabData: ['基本收费规则', '特殊收费规则'],
			currentTab: '基本收费规则',
			pageChargeList: {
				parkChargeBaseVoList: null
			},
			delRuleList: {},
			delRuleGroupList: [],
			dialogSetting: {
				ruleCalcDialogShow: false
			},
			curCalcItem: null,
			calcOpt: {
				parkUUID: this.$route.query.parkId,
				startTime: '',
				endTime: '',
				carType: '',
				chargeGroup: ''
			},
			carTypeOpt: [
				{
					name: '蓝牌车',
					value: 1
				},
				{
					name: '黄牌车',
					value: 2
				},
				{
					name: '新能源小车',
					value: 4
				},
				{
					name: '新能源大车',
					value: 3
				}
			],
			calcRules: {
				startTime: [{ required: true, message: '请选择进场时间', trigger: 'blur' }],
				endTime: [{ required: true, message: '请选择离场时间', trigger: 'blur' }]
			},
			calcResult: null
		};
	},
	created() {
		this.$route.query.tabName = this.currentTab;
		this.currentTab = this.$route.query.currentTabName ? this.$route.query.currentTabName : '基本收费规则';
		this.getOperatorParkBasicCharge();
	},
	methods: {
		changeTab(item) {
			this.currentTab = item;
			this.$route.query.tabName = this.currentTab;
			this.$router.push({
				name: 'allParkinglotInfo',
				query: Object.assign(this.$route.query, {
					currentTabName: this.currentTab
				})
			});
		},

		getOperatorParkBasicCharge() {
			const self = this;
			var params = {
				parkID: this.$route.query.parkId
			};
			API.fetchGet('/operatorPark/getOperatorParkBasicCharge', params).then(
				res => {
					if (res.data) {
						this.domain = res.data.domain;
						let uiOpt = {
							chargeRuleImgList: [
								{
									name: '停车场收费规则',
									url: res.data.domain + res.data.parkBaseInfo.chargeRulePicUrl
								}
							]
						};

						this.parkBaseInfo = Object.assign(res.data.parkBaseInfo, {
							uiOpt: uiOpt
						});

						this.originRule = JSON.parse(JSON.stringify(res.data.parkChargeBaseVoList));
						this.originData = JSON.parse(JSON.stringify(res.data));

						let defaultObj = null,
							defaultIdx = null;

						res.data.parkChargeBaseVoList.forEach((item, index) => {
							item.uiOpt = {
								isShow: item.isDefault ? true : false,
								carTypeOpt: this.carTypeOpt,
								ruleCarType:
									item.parkChargeStandardVoList.length === 1 &&
									item.parkChargeStandardVoList[0].carType === 0
										? '不区分'
										: '区分',
								curCarType: 1
							};

							if (item.parkChargeStandardVoList && item.parkChargeStandardVoList.length) {
								if (
									item.parkChargeStandardVoList[0].type === 1 &&
									item.parkChargeStandardVoList.length === 1
								) {
									item.isFree = '免费车场';
								} else {
									item.isFree = '收费车场';
								}
								item.parkChargeStandardVoList.forEach(citem => {
									let uiOpt = {
										splitTimeOpt: ['不分时段', '分时段'],
										curSplitTime:
											citem.parkChargeStandardPeriodVoList &&
											citem.parkChargeStandardPeriodVoList.length < 2
												? '不分时段'
												: '分时段',
										splitTimeCount: citem.parkChargeStandardPeriodVoList.length,
										isMaxCharge: citem.maxCharge !== null ? true : false,
										customIsMaxCharge:
											citem.customMaxCharge !== null && citem.customMaxUnit !== null
												? true
												: false,
										curTimeType: '分钟',
										isShowRuleDetail: item.isDefault ? true : false
									};

									citem.uiOpt = uiOpt;

									if (
										citem.parkChargeStandardPeriodVoList &&
										citem.parkChargeStandardPeriodVoList.length
									) {
										citem.parkChargeStandardPeriodVoList.forEach(ccitem => {
											let uiOpt = {
												ruleTypeOpt: ['按次定额收费', '按单价收费', '递增收费'],
												curRuleType: '按次定额收费',
												isMaxCharge: false,
												isShowMore: false,
												xunHuanRulesLength: ccitem.parkChargeStandardPeriodDetailVoList.filter(
													item => item.chargeType === 2 && item.stepType === 1
												).length
											};

											const curStyleItem = ccitem.parkChargeStandardPeriodDetailVoList[0];
											if (curStyleItem.chargeType === 3 && curStyleItem.chargeUnit === 1) {
												uiOpt.curRuleType = '按次定额收费';
											}

											if (curStyleItem.chargeType === 1) {
												uiOpt.curRuleType = '按单价收费';
												if (ccitem.maxCharge !== null && ccitem.maxCharge > 0) {
													uiOpt.isMaxCharge = true;
												}
											}

											if (curStyleItem.chargeType === 2 && curStyleItem.stepType === 1) {
												uiOpt.curRuleType = '递增收费';
												if (ccitem.maxCharge !== null && ccitem.maxCharge > 0) {
													uiOpt.isMaxCharge = true;
												}
											}
											ccitem.uiOpt = uiOpt;

											if (
												ccitem.parkChargeStandardPeriodDetailVoList &&
												ccitem.parkChargeStandardPeriodDetailVoList.length
											) {
												let fixedCharges = [];
												let uiOpt = {
													curTimeType: '分钟'
												};
												ccitem.parkChargeStandardPeriodDetailVoList.forEach(item => {
													item.uiOpt = uiOpt;
													if (item.chargeType === 2 && item.stepType === 1) {
														fixedCharges.push(item);
													}
												});
												ccitem.parkChargeStandardPeriodDetailVoList.fixedCharges = fixedCharges;
											}
										});
									}
								});
							} else {
								item.isFree = '免费车场';
							}

							if (item.isDefault) {
								defaultObj = deepCopyObj(item);
								defaultIdx = index;
							}
						});
						if (defaultIdx) {
							res.data.parkChargeBaseVoList.splice(defaultIdx, 1);
							res.data.parkChargeBaseVoList.unshift(defaultObj);
						}
						this.pageChargeList.parkChargeBaseVoList = res.data.parkChargeBaseVoList;
					}
				},
				err => {}
			);
		},

		expandAndCollapse(item, list) {
			const temp = item.uiOpt.isShow;
			if (temp) {
				list.forEach(citem => {
					citem.uiOpt.isShow = !temp;
				});
			} else {
				list.forEach(citem => {
					if (citem.chargeTypeSeq === item.chargeTypeSeq) {
						citem.uiOpt.isShow = !temp;
					} else {
						citem.uiOpt.isShow = temp;
					}
				});
			}
		},
		editRule(chargeTypeSeq) {
			this.$router.push({
				name: 'editRule',
				query: Object.assign(this.$route.query, {
					chargeTypeSeq,
					isAddRule: '',
					currentTabName: this.currentTab
				})
			});
		},
		addRuleGroup() {
			this.$router.push({
				name: 'editRule',
				query: Object.assign(this.$route.query, {
					isAddRule: true,
					chargeTypeSeq: '',
					currentTabName: this.currentTab
				})
			});
		},
		delRuleGroup(item) {
			if (item.isDefault) {
				this.$alert('默认收费规则不予许删除，请重新设置默认收费规则后重试', '删除提示', {
					confirmButtonText: '确定',
					confirmButtonClass: 'el-button el-button--danger alert-btn'
				});

				return;
			}
			this.$confirm(`确定删除收费规则“${item.typeName}”？`, '删除确认', {
				confirmButtonText: '确定',
				cancelButtonText: '取消',
				customClass: 'mgr-msg-box',
				confirmButtonClass: 'el-button el-button--danger'
			}).then(() => {
				item.actionType = 2;
				let saveData = {
					parkBaseInfo: this.parkBaseInfo,
					parkChargeBaseVoList: this.pageChargeList.parkChargeBaseVoList
				};
				API.fetchPost('/operatorPark/saveOperatorParkBasicCharge', saveData).then(
					res => {
						if (res.status === 1) {
							this.showToast('删除成功', 1, 1500);
							this.getOperatorParkBasicCharge();
						} else {
							this.showToast('删除成功!', 2, 1500);
						}
					},
					err => {
						console.log(err);
					}
				);
			});
		},
		linkToEditPic() {
			this.$router.push({
				name: 'editPic',
				query: Object.assign(this.$route.query, {
					currentTabName: this.currentTab
				})
			});
		},
		editSpecialRule(id) {
			this.$router.push({
				name: 'editSpecialRule',
				query: Object.assign(this.$route.query, {
					specialRuleId: id,
					isAddSpecialRule: '',
					currentTabName: this.currentTab
				})
			});
		},
		addSpecialRule() {
			this.$router.push({
				name: 'editSpecialRule',
				query: Object.assign(this.$route.query, {
					specialRuleId: '',
					isAddSpecialRule: true,
					currentTabName: this.currentTab
				})
			});
		},
		delSpecialRule(item) {
			const clone_item = deepCopyObj(item);
			this.$confirm(`确定删除特殊收费规则“${clone_item.chargeName}”？`, '删除确认', {
				confirmButtonText: '确定',
				cancelButtonText: '取消',
				customClass: 'mgr-msg-box',
				confirmButtonClass: 'el-button el-button--danger'
			}).then(() => {
				if (Array.isArray(clone_item.parkSpecialChargeServiceConditionVo.serviceConditionValue)) {
					clone_item.parkSpecialChargeServiceConditionVo.serviceConditionValue = clone_item.parkSpecialChargeServiceConditionVo.serviceConditionValue.join(
						','
					);
				}
				const saveData = [clone_item];
				API.fetchPost('/operatorPark/delParkSpecialChargeVo', saveData).then(
					res => {
						if (res.status === 1) {
							this.showToast('删除特殊收费规则成功', 1, 1500);
							this.getParkSpecialChargeVoList();
						} else {
							this.showToast(res.message, 2, 1500);
						}
					},
					err => {
						console.log(err);
					}
				);
			});
		},
		calc(item) {
			this.curCalcItem = item;
			this.dialogSetting.ruleCalcDialogShow = true;
			if (item.parkChargeStandardVoList.length === 1 && item.parkChargeStandardVoList[0].carType === 0) {
				this.calcOpt.carType = 0;
			} else {
				this.calcOpt.carType = 1;
			}

			this.calcOpt.chargeGroup = item.chargeTypeSeq;
			this.calcResult = null;
			this.calcOpt.startTime = '';
			this.calcOpt.endTime = '';
		},
		submitCalc() {
			this.$refs['calcOpt'].validate(valid => {
				if (valid) {
					API.fetchPost('operatorPark/chargeRuleCalc', this.calcOpt).then(res => {
						if (res.status === 1) {
							this.showToast('测算成功', 1, 1500);
							this.calcResult = res.data;
						} else {
							this.showToast('测算失败', 2, 1500);
						}
					});
				}
			});
		}
	},
	filters: {
		carType: function(value) {
			let rs = '';
			switch (value) {
				case 1:
					rs = '蓝牌车';
					break;
				case 2:
					rs = '黄牌车';
					break;
				case 4:
					rs = '新能源小车';
					break;
				case 3:
					rs = '新能源大车';
					break;
				case 0:
					rs = '不区分';
					break;
			}
			return rs;
		},
		mxSettleType: function(value) {
			if (value == 0) {
				return '自然结算';
			} else if (value == 1) {
				return '强制结算';
			}
		},
		natureDay: function(value) {
			if (value == 0) {
				return '停车时长';
			} else if (value == 1) {
				return '自然日';
			}
		},
		loopType: function(value) {
			if (value == 0) {
				return '循环末段收费规则';
			} else if (value == 1) {
				return '循环整个收费规则';
			}
		},
		chargeType: function(value) {
			if (value == 1) {
				return '24小时封顶';
			} else if (value == 5) {
				return '多次进出共用免费时长';
			}
		},
		institutionType: function(value) {
			if (value == 0) {
				return '停车时长';
			} else if (value == 1) {
				return '自然日';
			}
		},
		serviceConditionType: function(value) {
			if (value == 1) {
				return '<车牌>';
			} else if (value == 2) {
				return '<VIP类型>';
			} else if (value == 3) {
				return '<车辆类型>';
			}
		},
		serviceConditionValue: function(value) {
			let arr = [];
			let carTypes_special = [
				{
					type: '蓝牌车',
					value: '1'
				},
				{
					type: '黄牌车',
					value: '2'
				},
				{
					type: '新能源小车',
					value: '4'
				},
				{
					type: '新能源大车',
					value: '3'
				}
			];
			if (Array.isArray(value) && value.length) {
				for (let j = 0; j < value.length; j++) {
					for (let i = 0; i < carTypes_special.length; i++) {
						let obj = carTypes_special[i];
						if (obj.value == value[j]) {
							arr.push(obj.type);
							break;
						}
					}
				}
			}
			return arr.join(',');
		}
	}
};
